<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="设备编码" prop="deviceCode">
                <el-input class="w100p" v-model="ruleForm.deviceCode" placeholder="请输入设备编码"></el-input>
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceTypeId">
                <el-select v-model="ruleForm.deviceTypeId" class="w100p" clearable filterable placeholder="请选择设备类型">
                    <el-option v-for="item, index in deviceTypeList" :label="item.deviceTypeName"
                        :value="item.deviceTypeId" :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="设备地址">
                <el-input v-model="ruleForm.address" placeholder="请输入设备地址"></el-input>
            </el-form-item>
            <el-form-item label="纬度">
                <el-input type='number' v-model="ruleForm.lat" placeholder="请输入纬度"></el-input>
            </el-form-item>
            <el-form-item label="经度">
                <el-input type='number' v-model="ruleForm.lng" placeholder="请输入经度"></el-input>
            </el-form-item>
            <el-form-item label="设备描述">
                <el-input type="textarea" :rows="2" v-model="ruleForm.remark" placeholder="请输入设备描述"></el-input>
            </el-form-item>
            <el-form-item label="设备安装日期">
                <el-date-picker v-model="ruleForm.installDate" align="right" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                    :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>

import device from "@/api/device";
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        deviceTypeList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            addEditName: '添加',
            addEditApi: device.add,
            ruleForm: {},
            rules: {
                remark: [
                    { required: true, message: '请输入设备描述', trigger: 'change' }
                ],
                installDate: [
                    { required: true, message: '请选择安装日期', trigger: 'change' }
                ],
                deviceTypeId: [
                    { required: true, message: '请选择设备类型', trigger: 'change' }
                ],
                deviceCode: [
                    { required: true, message: '请输入设备编码', trigger: 'change' }
                ],
                address: [
                    { required: true, message: '请输入设备地址', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    computed: {},
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = device.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = device.edit
                this.addEditName = '编辑'
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi(this.ruleForm).then(res => {
                        if (res.code == 1) {
                            this.cancel()
                            this.$message.success('操作成功')
                        } else {
                            this.$message.warning(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>